package com.xiaobo.framework.repository.impl;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.xiaobo.framework.repository.MessageDao;
import com.xiaobo.framework.api.bean.Message;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author: <a href="get_xiaobo@163.com">Josh_Xu</a>
 * @description:
 * @create: 2020-04-24 15:33
 **/
@Component
public class MessageDaoImpl implements MessageDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    String time =  DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());


    @Override
    public List<Message> findListByFromAndTo(Long fromId, Long toId, Integer page, Integer rows) {

        /**
         * from->to信息记录
         */
        Criteria criteriaFrom = new Criteria().andOperator(
                Criteria.where("from.id").is(fromId),
                Criteria.where("to.id").is(toId)
        );
        /**
         * to->from消息记录
         */
        Criteria criteriaTo = new Criteria().andOperator(
                Criteria.where("from.id").is(toId),
                Criteria.where("to.id").is(fromId));

        Criteria criteria = new Criteria().orOperator(criteriaFrom, criteriaTo);
        /**
         * 分页
         */
        PageRequest pageRequest = PageRequest.of(page - 1, rows, Sort.by(Sort.Direction.ASC, "sendDate"));
        Query query = Query.query(criteria).with(pageRequest);
        return this.mongoTemplate.find(query, Message.class);
    }

    @Override
    public Message findMessageById(String id) {

        return this.mongoTemplate.findById(new ObjectId(id), Message.class);
    }

    @Override
    public UpdateResult updateMessageState(ObjectId id, Integer status) {

        Query query = Query.query(Criteria.where("id").is(id));
        Update update = Update.update("status", status);
        if(status.intValue()==0){
            update.set("send_date",time);
        }else if (status.intValue()==1){
            update.set("read_date",time);
        }
        return this.mongoTemplate.updateFirst(query,update,Message.class);
    }

    /**
     * status 1-已读
     * status 0-未读
     * @param message
     * @return
     */
    @Override
    public Message saveMessage(Message message) {
        message.setSendDate(time);
        message.setStatus(0);
        return this.mongoTemplate.save(message);
    }

    @Override
    public DeleteResult deleteMessage(String id) {
        Query query = Query.query(Criteria.where("id").is(id));
        return this.mongoTemplate.remove(query,Message.class);
    }
}
